from fontTools.ttLib import TTFont
from PIL import Image, ImageDraw, ImageFont
import io
from ddddocr import DdddOcr

ocr = DdddOcr(show_ad=False)

def get_font_maps(font_path):
    font_maps = {

    }
    font = TTFont(font_path)
    img_font = ImageFont.truetype(font_path, size=25)
    for code, name in font.getBestCmap().items():
        print(code, chr(code), name)

        img = Image.new("RGB", (32, 32), (255, 255, 255))
        draw = ImageDraw.Draw(img)
        draw.text((3, -3), chr(code), (0, 0, 0), font=img_font)

        img_bytes = io.BytesIO()
        img.save(img_bytes, format="JPEG")

        r = ocr.classification(img_bytes.getvalue())
        font_maps[code] = r

    print(font_maps)
    # return font_maps
if __name__ == '__main__':
    get_font_maps("../1.爬取网站/static/font/96fc7b50b772f52-700.woff2")
    # get_font_maps("../static/fonts/file")